package com.ss.android.article.base.ui;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.AbsListView;
import android.widget.ScrollView;

import com.ss.android.newmedia.activity.BaseActivity;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

public class SwipeBackLayout extends ViewGroup {
    @Retention(RetentionPolicy.SOURCE)
    public @interface DragEdge {
    }


    public interface a {
        void a();
    }

    public interface b {
        boolean a(MotionEvent arg1, int arg2);

        void a(int arg1, int arg2);

        void b(int arg1, int arg2);
    }

    final class c extends ViewDragHelper.Callback {
        private final SwipeBackLayout a;

        c(SwipeBackLayout arg1, byte arg2) {
            this(arg1);
        }

        private c(SwipeBackLayout arg1) {
            this.a = arg1;

        }

        public final int getViewVerticalDragRange() {
            return this.a.e;
        }

        public final int getViewHorizontalDragRange(View arg2) {
            return this.a.f;
        }

        public final void onViewDragStateChanged(int arg6) {
            if (arg6 != this.a.g) {
                if ((this.a.g == 1 || this.a.g == 2) && (arg6 == 0 && this.a.h == this.a.getDragRange())) {
                    if (this.a.b == null) {
                        Context v0 = this.a.getContext();
                        ((Activity) v0).findViewById(android.R.id.content).setVisibility(View.INVISIBLE);
                        ((Activity) v0).finish();
                        if ((v0 instanceof BaseActivity)) {
                            ((BaseActivity) v0).superOverridePendingTransition(0, android.R.anim.fade_out);
                        } else {
                            ((Activity) v0).overridePendingTransition(0, android.R.anim.fade_out);
                        }
                    } else {
                        this.a.b.a();
                    }
                }

                this.a.g = arg6;
            }
        }

        public final void onViewReleased(View arg9, float arg10, float arg11) {
            int v2_1;
            double v6 = 4000;
            int v0 = 1;
            int v1 = 0;
            if (this.a.h != 0 && this.a.h != this.a.getDragRange()) {
                SwipeBackLayout v2 = this.a;
                switch (v2.c) {
                    case 1: {
                        arg10 = -arg10;
                    }
                    case 0: {
                        if (Math.abs(arg10) <= Math.abs(arg11)) {
                            v2_1 = 0;
                            break;
                        } else if ((((double) arg10)) > v6) {
                            if (v2.c == 0) {
                                v2_1 = !v2.d() ? 1 : 0;
                            } else if (!v2.c()) {
                                v2_1 = 1;
                            } else {
                                v2_1 = 0;
                            }
                            break;
                        } else {
                            v2_1 = 0;
                            break;
                        }
                    }

                    case 3: {
                        arg11 = -arg11;
                    }
                    case 2: {
                        if (Math.abs(arg11) <= Math.abs(arg10)) {
                            v2_1 = 0;
                            break;
                        } else if ((((double) arg11)) > v6) {
                            if (v2.c == 2) {
                                v2_1 = !v2.a() ? 1 : 0;
                            } else if (!v2.b()) {
                                v2_1 = 1;
                            } else {
                                v2_1 = 0;
                            }
                            break;
                        } else {
                            v2_1 = 0;
                            break;
                        }
                    }
                    default: {
                        v2_1 = 0;
                        break;
                    }
                }
                if (v2_1 == 0 && (((float) this.a.h)) < this.a.getFinishAnchor()) {
                    v0 = 0;
                }
                switch (this.a.c) {
                    case 0: {
                        if (v0 != 0) {
                            v1 = this.a.f;
                        }
                        this.a.a(v1);
                        return;
                    }
                    case 1: {
                        if (v0 != 0) {
                            v1 = -this.a.f;
                        }
                        this.a.a(v1);
                        return;
                    }
                    case 2: {
                        if (v0 != 0) {
                            v1 = this.a.e;
                        }
                        this.a.b(v1);
                        return;
                    }
                    case 3: {
                        if (v0 != 0) {
                            v1 = -this.a.e;
                        }
                        this.a.b(v1);
                        return;
                    }
                }
                return;


            }
        }

        public final void onViewPositionChanged(View arg5, int arg6, int arg7) {
            switch (this.a.c) {
                case 0:
                case 1: {
                    this.a.h = Math.abs(arg6);
                    this.a.setBackgroundColor(0);
                    break;
                }
                case 2:
                case 3: {
                    this.a.h = Math.abs(arg7);
                    if (!this.a.a) {
                        return;
                    }

                    this.a.setBackgroundColor(SwipeBackLayout.a(1f - (((float) this.a.h)) / (((float) this.a.e))));
                    break;
                }
            }
        }

        public final boolean tryCaptureView(View arg2, int arg3) {
            boolean v0 = arg2 == this.a.d ? true : false;
            return v0;
        }

        public final int clampViewPositionVertical(View arg3, int arg4) {
            int v0;
            if (this.a.c != 2 || (this.a.a()) || arg4 <= 0) {
                if (this.a.c == 3 && !this.a.b() && arg4 < 0) {
                    return Math.min(Math.max(arg4, -this.a.e), this.a.getPaddingTop());
                }

                v0 = 0;
            } else {
                v0 = Math.min(Math.max(arg4, this.a.getPaddingTop()), this.a.e);
            }

            return v0;
        }

        public final int clampViewPositionHorizontal(View arg3, int arg4) {
            int v0;
            if (this.a.c != 0 || (this.a.c()) || arg4 <= 0) {
                if (this.a.c == 1 && !this.a.d() && arg4 < 0) {
                    return Math.min(Math.max(arg4, -this.a.f), this.a.getPaddingLeft());
                }

                v0 = 0;
            } else {
                v0 = Math.min(Math.max(arg4, this.a.getPaddingLeft()), this.a.f);
            }

            return v0;
        }
    }

    boolean a;
    a b;
    int c;
    View d;
    int e;
    int f;
    int g;
    int h;
    private int i;
    private float j;
    private float k;
    private boolean l;
    private boolean m;
    private boolean n;
    private final ViewDragHelper o;
    private View p;
    private float q;
    private float r;
    private boolean s;
    private b t;

    public SwipeBackLayout(Context arg2) {
        this(arg2, null);
    }

    public SwipeBackLayout(Context arg3, AttributeSet arg4) {
        super(arg3, arg4);
        this.a = true;
        this.c = 2;
        this.q = 0f;
        this.r = 0f;
        this.i = ViewConfiguration.get(arg3).getScaledTouchSlop();
        this.o = ViewDragHelper.create(((ViewGroup) this), new c(this, (byte) 0));
    }

    static int a(float arg2) {
        return Color.argb(((int) (255f * arg2)), 0, 0, 0);
    }

    private void a(ViewGroup arg5) {
        View v2;
        this.p = ((View) arg5);
        if (arg5.getChildCount() > 0) {
            int v1 = arg5.getChildCount();
            int v0 = 0;
            while (true) {
                if (v0 < v1) {
                    v2 = arg5.getChildAt(v0);
                    if (!(v2 instanceof AbsListView) && !(v2 instanceof ScrollView) && !(v2 instanceof ViewPager) && !(v2 instanceof WebView)) {
                        ++v0;
                        continue;
                    }

                    break;
                }

                return;
            }

            this.p = v2;
        }
    }

    final void a(int arg3) {
        if (this.o.settleCapturedViewAt(arg3, 0)) {
            ViewCompat.postInvalidateOnAnimation(((View) this));
        }
    }

    public final boolean a() {
        return ViewCompat.canScrollVertically(this.p, -1);
    }

    final void b(int arg3) {
        if (this.o.settleCapturedViewAt(0, arg3)) {
            ViewCompat.postInvalidateOnAnimation(((View) this));
        }
    }

    public final boolean b() {
        return ViewCompat.canScrollVertically(this.p, 1);
    }

    final boolean c() {
        return ViewCompat.canScrollHorizontally(this.p, -1);
    }

    public void computeScroll() {
        if (this.o.continueSettling(true)) {
            ViewCompat.postInvalidateOnAnimation(((View) this));
        }
    }

    final boolean d() {
        return ViewCompat.canScrollHorizontally(this.p, 1);
    }

    public boolean dispatchTouchEvent(MotionEvent arg3) {
        int v0 = arg3.getActionMasked();
        if (v0 == 1 || v0 == 3) {
            this.m = false;
        }

        return super.dispatchTouchEvent(arg3);
    }

    int getDragRange() {
        int v0;
        switch (this.c) {
            case 0:
            case 1: {
                v0 = this.f;
                break;
            }
            case 2:
            case 3: {
                v0 = this.e;
                break;
            }
            default: {
                v0 = this.e;
                break;
            }
        }

        return v0;
    }

    float getFinishAnchor() {
        float v0;
        switch (this.c) {
            case 0:
            case 1: {
                v0 = this.r;
                break;
            }
            case 2:
            case 3: {
                v0 = this.q;
                break;
            }
            default: {
                v0 = this.q;
                break;
            }
        }

        return v0;
    }

    public boolean onInterceptTouchEvent(MotionEvent arg13) {
        int v0_2;
        boolean v0;
        int v4 = 3;
        int v3 = 2;
        if (!this.isEnabled() || (this.m)) {
            v0 = false;
            return v0;
        }

        int v5 = arg13.getActionMasked();
        if (v5 == 5) {
            this.m = true;
            return false;
        }

        try {
            if (this.d == null) {
                if (this.getChildCount() > 1) {
                    throw new IllegalStateException("SwipeBackLayout must contains only one direct child");
                } else {
                    this.d = this.getChildAt(0);
                    if (this.p == null && this.d != null) {
                        if ((this.d instanceof ViewGroup)) {
                            this.a((ViewGroup) this.d);
                        } else {
                            this.p = this.d;
                        }
                    }
                }
            }

            switch (v5) {
                case 0: {
                    this.j = arg13.getRawX();
                    this.k = arg13.getRawY();
                    this.l = true;
                    v0_2 = 0;
                    break;
                }
                case 2: {
                    if (!this.l) {
                        v0_2 = 0;
                        break;
                    }

                    this.l = false;
                    float v0_3 = Math.abs(arg13.getRawX() - this.j);
                    float v6 = Math.abs(arg13.getRawY() - this.k);
                    if (Math.hypot(((double) v0_3), ((double) v6)) < (((double) this.i))) {
                        this.l = true;
                        v0_2 = 1;
                        break;
                    }

                    if (v6 <= v0_3) {
                        this.c = 0;
                        v0_2 = 0;
                        break;
                    }

                    if (arg13.getRawY() > this.k) {
                        v0_2 = v3;
                    } else {
                        v0_2 = v4;
                    }
                    this.c = v0_2;
                    v0_2 = 0;
                    break;
                }
                default: {
                    v0_2 = 0;
                    break;

                }
            }

            label_41:
            if (v0_2 != 0) {
                return this.o.shouldInterceptTouchEvent(arg13);
            }

            v0_2 = this.c;
            v0 = this.t == null ? true : this.t.a(arg13, v0_2);
            if (v0) {
                if ((this.n) && v5 == v3 && (this.c == v3 || this.c == v4)) {
                    this.s = true;
                    this.setBackgroundColor(0);
                    this.o.cancel();
                    return true;
                }

                return this.o.shouldInterceptTouchEvent(arg13);
            }

            this.o.cancel();
        } catch (Exception v0_1) {
        }

        return false;
    }

    protected void onLayout(boolean arg6, int arg7, int arg8, int arg9, int arg10) {
        int v0 = this.getMeasuredWidth();
        int v1 = this.getMeasuredHeight();
        if (this.getChildCount() != 0) {
            View v2 = this.getChildAt(0);
            v0 = v0 - this.getPaddingLeft() - this.getPaddingRight();
            v1 = v1 - this.getPaddingTop() - this.getPaddingBottom();
            int v3 = this.getPaddingLeft();
            int v4 = this.getPaddingTop();
            v2.layout(v3, v4, v0 + v3, v1 + v4);
        }
    }

    protected void onMeasure(int arg5, int arg6) {
        int v3 = MeasureSpec.EXACTLY;
        super.onMeasure(arg5, arg6);
        if (this.getChildCount() > 1) {
            throw new IllegalStateException("SwipeBackLayout must contains only one direct child.");
        }

        if (this.getChildCount() > 0) {
            this.getChildAt(0).measure(
                    View.MeasureSpec.makeMeasureSpec(this.getMeasuredWidth() - this.getPaddingLeft() - this.getPaddingRight(), v3),
                    View.MeasureSpec.makeMeasureSpec(this.getMeasuredHeight() - this.getPaddingTop() - this.getPaddingBottom(), v3));
        }
    }

    protected void onSizeChanged(int arg4, int arg5, int arg6, int arg7) {
        float v2 = 0.25f;
        super.onSizeChanged(arg4, arg5, arg6, arg7);
        this.e = arg5;
        this.f = arg4;
        float v0 = this.q > 0f ? this.q : (((float) this.e)) * v2;
        this.q = v0;
        v0 = this.r > 0f ? this.r : (((float) this.f)) * v2;
        this.r = v0;
    }

    public boolean onTouchEvent(MotionEvent arg6) {
        if ((this.n) && (this.s)) {
            int v0 = ((int) (arg6.getRawY() - this.k));
            int v1 = ((int) (255f * Math.max(1f - (((float) Math.abs(v0))) * 1.5f / (((float) this.getHeight())), 0f)));
            switch (arg6.getActionMasked()) {
                case 1: {
                    this.s = false;
                    if (this.t != null) {
                        this.t.b(v0, v1);
                    } else {
                    }
                    return true;
                }
                case 2: {
                    if (this.t != null) {
                        this.t.a(v0, v1);
                        return true;
                    } else {
                    }
                    return true;
                }
            }

            return true;

        }

        try {
            this.o.processTouchEvent(arg6);
        } catch (Exception v0_1) {
        }

        return true;
    }

    public void setDrawShadow(boolean arg1) {
        this.a = arg1;
    }

    public void setOnFinishListener(a arg1) {
        this.b = arg1;
    }

    public void setSwipeBackDelegate(b arg1) {
        this.t = arg1;
    }

    public void setTransparencyEnabled(boolean arg1) {
        this.n = arg1;
    }
}

